package com.taobao.taobaowidget.popupspinner.adapter;

import java.util.List;

import android.content.Context;
import android.text.TextUtils.TruncateAt;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

import com.taobao.taobaowidget.popupspinner.BackgroundView;
import com.taobao.taobaowidget.popupspinner.data.BaseDataInterface;


public class PopupImageAdapter extends BaseAdapter{
	
	private Context mContext;
	private List<BaseDataInterface> mListData;
	private int mBGColor;
	private int mTextColor;
    private int mSelectedBGColor;
    private int mSelectedTextColor;
    private int mTextSize;
    private final int IMAGEVIEW_ID = 1;
    
    
	public PopupImageAdapter(Context context, List<BaseDataInterface> data, int bgcolor, int textcolor, int textsize, int selectedbgcolor, int selectedtextcolor){
		this.mContext = context;
		this.mListData = data;
		this.mBGColor = bgcolor;
		this.mTextColor = textcolor;
		this.mTextSize = textsize;
		this.mSelectedBGColor = selectedbgcolor;
		this.mSelectedTextColor = selectedtextcolor;
	}
	
	public int getCount() {
		// TODO Auto-generated method stub
		return mListData.size();
	}

	public Object getItem(int pos) {
		// TODO Auto-generated method stub
		return mListData.get(pos);
	}
	
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public View getView(int pos, View view, ViewGroup group){
		// TODO Auto-generated method stub
		ViewHolder holder = null;
		if(view == null){
			holder =  new ViewHolder();
			RelativeLayout rl = new RelativeLayout(mContext);
			rl.setPadding(8, 8, 8, 8);
			rl.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.FILL_PARENT, 96));
			rl.setBackgroundDrawable(BackgroundView.getDrawable(mBGColor, mSelectedBGColor));
			RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(72, 72);
			ImageView iv = new ImageView(mContext);
			lp.setMargins(0, 0, 8, 0);
			iv.setLayoutParams(lp);
			iv.setId(IMAGEVIEW_ID);
			rl.addView(iv);
			
			TextView tv =  new TextView(mContext);
			RelativeLayout.LayoutParams RlayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
			RlayoutParams.addRule(RelativeLayout.RIGHT_OF, IMAGEVIEW_ID);
//			tv.setLayoutParams(RlayoutParams);
			tv.setMaxWidth(170);
			tv.setTextSize(mTextSize);
			tv.setGravity(Gravity.CENTER_VERTICAL);
			tv.setSingleLine();
			tv.setTextColor(BackgroundView.getColorStateList(mTextColor, mSelectedTextColor));
			tv.setGravity(Gravity.CENTER);
			rl.addView(tv, RlayoutParams);
			view = rl;
			holder.mHolderImage = iv ;
			holder.mHolderText = tv;
		}else{
			holder = (ViewHolder)view.getTag();
		}
		BaseDataInterface basedata = mListData.get(pos);
		holder.mHolderText.setText(basedata.getString());
		holder.mHolderImage.setBackground(basedata.getDrawable());
		view.setTag(holder);
		return view;
	}
}

class ViewHolder{
	TextView mHolderText;
	ImageView mHolderImage;
}
